package com.muyan.common.bean;

import com.muyan.common.kits.StrKit;

import java.io.Serializable;

/**
 * 返回的结果实体
 *
 * @author zzl 2018/03/19
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 4923514283387322952L;

    public boolean succ;
    public String msg;
    public String subMsg;
    public T data;
    public String code;
    public String subCode;

    public Result() {
    }

    public Result(boolean succ) {
        this.succ = succ;
    }

    public static <T> Result<T> succ(T data) {
        Result<T> result = new Result<>(true);
        result.data = data;
        return result;
    }

    public static <T> Result<T> succ(T data, String msg, Object... msgArgs) {
        Result<T> result = new Result<>(true);
        result.data = data;
        if (msgArgs == null || msgArgs.length == 0) {
            result.msg = msg;
        } else {
            result.msg = StrKit.format(msg, msgArgs);
        }
        return result;
    }

    public static <T> Result<T> fail(String msg, Object... msgArgs) {
        Result<T> result = new Result<>(false);
        if (msgArgs == null || msgArgs.length == 0) {
            result.msg = msg;
        } else {
            result.msg = StrKit.format(msg, msgArgs);
        }
        return result;
    }

    public Result<T> data(T data) {
        this.data = data;
        return this;
    }

    public Result<T> msg(String msg, Object... msgArgs) {
        if (msgArgs == null || msgArgs.length == 0) {
            this.msg = msg;
        } else {
            this.msg = StrKit.format(msg, msgArgs);
        }
        return this;
    }

    public Result<T> subMsg(String subMsg) {
        this.subMsg = subMsg;
        return this;
    }

    public Result<T> code(String code) {
        this.code = code;
        return this;
    }

    public Result<T> subCode(String subCode) {
        this.subCode = subCode;
        return this;
    }

    public boolean isSucc() {
        return succ;
    }

    public void setSucc(boolean succ) {
        this.succ = succ;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getSubMsg() {
        return subMsg;
    }

    public void setSubMsg(String subMsg) {
        this.subMsg = subMsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getSubCode() {
        return subCode;
    }

    public void setSubCode(String subCode) {
        this.subCode = subCode;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Result{");
        sb.append("succ=").append(succ);
        sb.append(", msg='").append(msg).append('\'');
        sb.append(", subMsg='").append(subMsg).append('\'');
        sb.append(", data=").append(data);
        sb.append(", code=").append(code);
        sb.append(", subCode=").append(subCode);
        sb.append('}');
        return sb.toString();
    }
}
